import swaggerJSDoc from 'swagger-jsdoc';
import { SwaggerDefinition } from 'swagger-jsdoc';

// Swagger配置
const swaggerDefinition: SwaggerDefinition = {
  openapi: '3.0.0',
  info: {
    title: '股票分析服务系统 API',
    version: '1.0.0',
    description: '基于Node.js和TypeScript的股票数据分析后端服务API文档',
    contact: {
      name: 'API Support',
      email: 'support@example.com'
    },
    license: {
      name: 'MIT',
      url: 'https://opensource.org/licenses/MIT'
    }
  },
  servers: [
    {
      url: 'http://47.110.91.156:3129',
      description: '生产环境'
    },
    {
      url: `http://localhost:${process.env.PORT || 3129}`,
      description: '开发环境'
    }
  ],
  components: {
    securitySchemes: {
      BearerAuth: {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
        description: 'JWT令牌认证，格式: Bearer <token>'
      }
    },
    schemas: {
      // ===== 通用响应模型 =====
      ApiResponse: {
        type: 'object',
        properties: {
          success: {
            type: 'boolean',
            description: '请求是否成功'
          },
          message: {
            type: 'string',
            description: '响应消息'
          },
          data: {
            description: '响应数据，具体类型请参考各接口的专门定义'
          },
          timestamp: {
            type: 'string',
            format: 'date-time',
            description: '响应时间戳'
          }
        }
      },

      // 错误响应模型
      ErrorResponse: {
        type: 'object',
        properties: {
          success: {
            type: 'boolean',
            description: '请求是否成功',
            example: false
          },
          message: {
            type: 'string',
            description: '错误消息'
          },
          timestamp: {
            type: 'string',
            format: 'date-time',
            description: '响应时间戳'
          },
          error: {
            type: 'string',
            description: '错误详情（仅开发环境返回）'
          },
          stack: {
            type: 'string',
            description: '错误堆栈（仅开发环境返回）'
          }
        },
        required: ['success', 'message', 'timestamp']
      },

      // 验证错误响应模型
      ValidationErrorResponse: {
        type: 'object',
        properties: {
          success: {
            type: 'boolean',
            description: '请求是否成功',
            example: false
          },
          message: {
            type: 'string',
            description: '错误消息',
            example: '数据验证失败'
          },
          errors: {
            oneOf: [
              {
                type: 'array',
                items: { type: 'object' },
                description: '验证错误详情数组'
              },
              {
                type: 'object',
                description: '验证错误详情对象'
              }
            ]
          },
          timestamp: {
            type: 'string',
            format: 'date-time',
            description: '响应时间戳'
          }
        },
        required: ['success', 'message', 'errors', 'timestamp']
      },

      // 分页响应模型
      PaginatedResponse: {
        type: 'object',
        properties: {
          success: {
            type: 'boolean',
            description: '请求是否成功'
          },
          message: {
            type: 'string',
            description: '响应消息'
          },
          data: {
            type: 'array',
            description: '分页数据列表'
          },
          pagination: {
            $ref: '#/components/schemas/Pagination'
          },
          timestamp: {
            type: 'string',
            format: 'date-time',
            description: '响应时间戳'
          }
        }
      },

      // ===== API 响应数据类型定义 =====
      
      // 股票实时数据响应
      StockRealtimeResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                $ref: '#/components/schemas/Stock'
              }
            }
          }
        ]
      },

      // 股票历史数据响应
      StockHistoryResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'array',
                items: {
                  $ref: '#/components/schemas/StockDailyData'
                }
              }
            }
          }
        ]
      },

      // 股票评分响应
      StockRatingResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                $ref: '#/components/schemas/StockRating'
              }
            }
          }
        ]
      },

      // 股票搜索响应
      StockSearchResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'array',
                items: {
                  $ref: '#/components/schemas/Stock'
                }
              }
            }
          }
        ]
      },

      // 市场概览响应
      MarketOverviewResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'array',
                items: {
                  $ref: '#/components/schemas/MainCategoryOverview'
                }
              }
            }
          }
        ]
      },

      // 市场评分响应
      MarketRatingResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                $ref: '#/components/schemas/MarketRatingData'
              }
            }
          }
        ]
      },

      // 涨跌分布响应
      RiseFallDistributionResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                $ref: '#/components/schemas/MarketRiseFallAnalysis'
              }
            }
          }
        ]
      },

      // 大分类列表响应
      MainCategoriesResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'object',
                properties: {
                  categories: {
                    type: 'array',
                    items: {
                      $ref: '#/components/schemas/MainCategoryInfo'
                    }
                  }
                }
              }
            }
          }
        ]
      },

      // 小分类列表响应
      SubCategoriesResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'object',
                properties: {
                  mainCategory: {
                    $ref: '#/components/schemas/MainCategoryInfo'
                  },
                  subCategories: {
                    type: 'array',
                    items: {
                      $ref: '#/components/schemas/SubCategoryInfo'
                    }
                  }
                }
              }
            }
          }
        ]
      },

      // 小分类股票列表响应
      CategoryStocksResponse: {
        allOf: [
          { $ref: '#/components/schemas/PaginatedResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'object',
                properties: {
                  data: {
                    type: 'array',
                    items: {
                      $ref: '#/components/schemas/Stock'
                    }
                  },
                  pagination: {
                    $ref: '#/components/schemas/Pagination'
                  },
                  categoryInfo: {
                    type: 'object',
                    properties: {
                      mainCategory: {
                        type: 'string',
                        example: 'a_stock'
                      },
                      subCategory: {
                        type: 'string',
                        example: 'sh_a'
                      },
                      name: {
                        type: 'string',
                        example: '上证A股'
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      },

      // 大分类所有股票列表响应
      MainCategoryAllStocksResponse: {
        allOf: [
          { $ref: '#/components/schemas/PaginatedResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'object',
                properties: {
                  data: {
                    type: 'array',
                    items: {
                      $ref: '#/components/schemas/Stock'
                    }
                  },
                  pagination: {
                    $ref: '#/components/schemas/Pagination'
                  },
                  categoryInfo: {
                    type: 'object',
                    properties: {
                      mainCategory: {
                        type: 'string',
                        example: 'a_stock',
                        description: '大分类键值'
                      },
                      name: {
                        type: 'string',
                        example: 'A股市场',
                        description: '大分类名称'
                      },
                      description: {
                        type: 'string',
                        example: '中国内地A股市场',
                        description: '大分类描述'
                      },
                      subCategoryCount: {
                        type: 'integer',
                        example: 5,
                        description: '包含的小分类数量'
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      },

      // 分类统计响应
      CategoryStatsResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'array',
                items: {
                  $ref: '#/components/schemas/MainCategoryStats'
                }
              }
            }
          }
        ]
      },

      // 小分类统计响应
      SubCategoryStatsResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                type: 'object',
                properties: {
                  mainCategory: {
                    type: 'object',
                    properties: {
                      key: {
                        type: 'string',
                        example: 'a_stock'
                      },
                      name: {
                        type: 'string',
                        example: 'A股市场'
                      }
                    }
                  },
                  subCategory: {
                    type: 'object',
                    properties: {
                      key: {
                        type: 'string',
                        example: 'sh_a'
                      },
                      name: {
                        type: 'string',
                        example: '上证A股'
                      },
                      description: {
                        type: 'string',
                        example: '上海证券交易所A股'
                      },
                      stockCount: {
                        type: 'integer',
                        example: 1800
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      },

      // ===== 业务数据模型定义 =====
      
      // 分页模型
      Pagination: {
        type: 'object',
        properties: {
          pageIndex: {
            type: 'integer',
            description: '当前页码（从1开始）',
            example: 1
          },
          pageSize: {
            type: 'integer',
            description: '每页条数',
            example: 20
          },
          total: {
            type: 'integer',
            description: '总条数',
            example: 100
          },
          totalPages: {
            type: 'integer',
            description: '总页数',
            example: 5
          }
        }
      },
      
      // 市场模型
      Market: {
        type: 'object',
        properties: {
          id: {
            type: 'integer',
            description: '市场ID'
          },
          marketCode: {
            type: 'string',
            description: '市场代码',
            enum: ['SH', 'SZ', 'HK', 'US', 'UK', 'NEEQ']
          },
          marketName: {
            type: 'string',
            description: '市场名称'
          },
          currency: {
            type: 'string',
            description: '交易货币'
          },
          timezone: {
            type: 'string',
            description: '时区'
          },
          tradingHours: {
            type: 'object',
            description: '交易时间配置'
          },
          status: {
            type: 'integer',
            description: '状态 1-正常 0-禁用'
          }
        }
      },
      
      // 股票模型
      Stock: {
        type: 'object',
        properties: {
          id: {
            type: 'integer',
            description: '股票ID'
          },
          stockCode: {
            type: 'string',
            description: '股票代码'
          },
          stockName: {
            type: 'string',
            description: '股票名称'
          },
          marketId: {
            type: 'integer',
            description: '市场ID'
          },
          industry: {
            type: 'string',
            nullable: true,
            description: '行业'
          },
          sector: {
            type: 'string',
            nullable: true,
            description: '板块'
          },
          listingDate: {
            type: 'string',
            format: 'date',
            nullable: true,
            description: '上市日期'
          },
          totalShares: {
            type: 'string',
            nullable: true,
            description: '总股本'
          },
          outstandingShares: {
            type: 'string',
            nullable: true,
            description: '流通股本'
          },
          status: {
            type: 'integer',
            description: '状态 1-正常 0-禁用'
          },
          createdAt: {
            type: 'string',
            format: 'date-time',
            description: '创建时间'
          },
          updatedAt: {
            type: 'string',
            format: 'date-time',
            description: '更新时间'
          },
          market: {
            $ref: '#/components/schemas/Market'
          },
          realtime: {
            $ref: '#/components/schemas/StockRealtime'
          }
        }
      },
      
      // 股票实时数据模型
      StockRealtime: {
        type: 'object',
        properties: {
          id: {
            type: 'integer',
            description: '实时数据ID'
          },
          stockId: {
            type: 'integer',
            description: '股票ID'
          },
          currentPrice: {
            type: 'string',
            description: '现价'
          },
          openPrice: {
            type: 'string',
            nullable: true,
            description: '开盘价'
          },
          closePrice: {
            type: 'string',
            nullable: true,
            description: '昨收价'
          },
          highPrice: {
            type: 'string',
            nullable: true,
            description: '最高价'
          },
          lowPrice: {
            type: 'string',
            nullable: true,
            description: '最低价'
          },
          volume: {
            type: 'string',
            nullable: true,
            description: '成交量'
          },
          turnover: {
            type: 'string',
            nullable: true,
            description: '成交额'
          },
          changeAmount: {
            type: 'string',
            nullable: true,
            description: '涨跌额'
          },
          changePercent: {
            type: 'string',
            nullable: true,
            description: '涨跌幅%'
          },
          peRatio: {
            type: 'string',
            nullable: true,
            description: '市盈率'
          },
          pbRatio: {
            type: 'string',
            nullable: true,
            description: '市净率'
          },
          marketCap: {
            type: 'string',
            nullable: true,
            description: '总市值'
          },
          circulationMarketCap: {
            type: 'string',
            nullable: true,
            description: '流通市值'
          },
          updateTime: {
            type: 'string',
            format: 'date-time',
            description: '更新时间'
          }
        }
      },
      
      // 历史数据模型
      StockDailyData: {
        type: 'object',
        properties: {
          id: {
            type: 'integer',
            description: '历史数据ID'
          },
          stockId: {
            type: 'integer',
            description: '股票ID'
          },
          tradeDate: {
            type: 'string',
            format: 'date',
            description: '交易日期'
          },
          openPrice: {
            type: 'string',
            description: '开盘价'
          },
          highPrice: {
            type: 'string',
            description: '最高价'
          },
          lowPrice: {
            type: 'string',
            description: '最低价'
          },
          closePrice: {
            type: 'string',
            description: '收盘价'
          },
          volume: {
            type: 'string',
            description: '成交量'
          },
          turnover: {
            type: 'string',
            description: '成交额'
          },
          changeAmount: {
            type: 'string',
            nullable: true,
            description: '涨跌额'
          },
          changePercent: {
            type: 'string',
            nullable: true,
            description: '涨跌幅%'
          },
          createdAt: {
            type: 'string',
            format: 'date-time',
            description: '创建时间'
          }
        }
      },
      
      // 股票评分模型
      StockRating: {
        type: 'object',
        properties: {
          id: {
            type: 'integer',
            description: '评分ID'
          },
          stockId: {
            type: 'integer',
            description: '股票ID'
          },
          overallScore: {
            type: 'integer',
            nullable: true,
            description: '综合评分 1-10分'
          },
          technicalScore: {
            type: 'integer',
            nullable: true,
            description: '技术分析评分'
          },
          fundamentalScore: {
            type: 'integer',
            nullable: true,
            description: '基本面评分'
          },
          marketScore: {
            type: 'integer',
            nullable: true,
            description: '市场表现评分'
          },
          ratingDate: {
            type: 'string',
            format: 'date',
            description: '评分日期'
          },
          createdAt: {
            type: 'string',
            format: 'date-time',
            description: '创建时间'
          }
        }
      },

      // 大分类概览数据模型
      MainCategoryOverview: {
        type: 'object',
        properties: {
          key: {
            type: 'string',
            example: 'a_stock',
            description: '大分类键值'
          },
          name: {
            type: 'string',
            example: 'A股市场',
            description: '大分类名称'
          },
          description: {
            type: 'string',
            example: '中国内地A股市场',
            description: '大分类描述'
          },
          totalStocks: {
            type: 'integer',
            example: 4500,
            description: '总股票数量'
          },
          activeStocks: {
            type: 'integer',
            example: 4200,
            description: '活跃股票数量'
          },
          avgChangePercent: {
            type: 'number',
            example: 1.25,
            description: '平均涨跌幅'
          },
          subCategories: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                key: {
                  type: 'string',
                  example: 'sh_a'
                },
                name: {
                  type: 'string',
                  example: '上证A股'
                },
                totalStocks: {
                  type: 'integer',
                  example: 1800
                },
                activeStocks: {
                  type: 'integer',
                  example: 1750
                },
                avgChangePercent: {
                  type: 'number',
                  example: 0.85
                }
              }
            }
          }
        }
      },

      // 市场评分数据模型
      MarketRatingData: {
        type: 'object',
        properties: {
          rating: {
            type: 'integer',
            minimum: 1,
            maximum: 10,
            example: 7,
            description: '市场评分（1-10分）'
          },
          description: {
            type: 'string',
            example: '乐观',
            description: '评分描述'
          },
          mainCategory: {
            $ref: '#/components/schemas/MarketCategory'
          },
          statistics: {
            type: 'object',
            properties: {
              totalStocks: {
                type: 'integer',
                example: 4000,
                description: '总股票数量'
              },
              riseCount: {
                type: 'integer',
                example: 2400,
                description: '上涨股票数量'
              },
              fallCount: {
                type: 'integer',
                example: 1400,
                description: '下跌股票数量'
              },
              flatCount: {
                type: 'integer',
                example: 200,
                description: '平盘股票数量'
              },
              riseRatio: {
                type: 'number',
                example: 60.5,
                description: '上涨比例%'
              },
              avgChange: {
                type: 'number',
                example: 1.25,
                description: '平均涨跌幅%'
              }
            }
          }
        }
      },

      // 大分类信息模型
      MainCategoryInfo: {
        type: 'object',
        properties: {
          key: {
            type: 'string',
            example: 'a_stock',
            description: '大分类键值'
          },
          name: {
            type: 'string',
            example: 'A股市场',
            description: '大分类名称'
          },
          description: {
            type: 'string',
            example: '中国内地A股市场',
            description: '大分类描述'
          },
          subCategoryCount: {
            type: 'integer',
            example: 5,
            description: '子分类数量'
          }
        }
      },

      // 小分类信息模型
      SubCategoryInfo: {
        type: 'object',
        properties: {
          key: {
            type: 'string',
            example: 'sh_a',
            description: '小分类键值'
          },
          name: {
            type: 'string',
            example: '上证A股',
            description: '小分类名称'
          },
          description: {
            type: 'string',
            example: '上海证券交易所A股',
            description: '小分类描述'
          }
        }
      },

      // 大分类统计模型
      MainCategoryStats: {
        type: 'object',
        properties: {
          key: {
            type: 'string',
            example: 'a_stock',
            description: '大分类键值'
          },
          name: {
            type: 'string',
            example: 'A股市场',
            description: '大分类名称'
          },
          totalStockCount: {
            type: 'integer',
            example: 4500,
            description: '总股票数量'
          },
          subCategories: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                key: {
                  type: 'string',
                  example: 'sh_a'
                },
                name: {
                  type: 'string',
                  example: '上证A股'
                },
                stockCount: {
                  type: 'integer',
                  example: 1800
                }
              }
            }
          }
        }
      },
      
      // 涨跌分布模型
      RiseFallDistribution: {
        type: 'object',
        description: '涨跌幅区间分布统计',
        properties: {
          rise_over_5: {
            type: 'integer',
            description: '涨幅大于5%的股票数量'
          },
          rise_3_to_5: {
            type: 'integer',
            description: '涨幅3%-5%的股票数量'
          },
          rise_1_to_3: {
            type: 'integer',
            description: '涨幅1%-3%的股票数量'
          },
          rise_0_to_1: {
            type: 'integer',
            description: '涨幅0%-1%的股票数量'
          },
          fall_0_to_1: {
            type: 'integer',
            description: '跌幅0%-1%的股票数量'
          },
          fall_1_to_3: {
            type: 'integer',
            description: '跌幅1%-3%的股票数量'
          },
          fall_3_to_5: {
            type: 'integer',
            description: '跌幅3%-5%的股票数量'
          },
          fall_over_5: {
            type: 'integer',
            description: '跌幅大于5%的股票数量'
          }
        }
      },
      
      // 市场类别信息模型
      MarketCategory: {
        type: 'object',
        properties: {
          key: {
            type: 'string',
            description: '市场类别键值',
            example: 'a_stock'
          },
          name: {
            type: 'string',
            description: '市场类别名称',
            example: 'A股市场'
          }
        }
      },
      
      // 市场涨跌分析响应模型
      MarketRiseFallAnalysis: {
        type: 'object',
        properties: {
          mainCategory: {
            $ref: '#/components/schemas/MarketCategory'
          },
          limitUp: {
            type: 'integer',
            description: '涨停股票数量'
          },
          limitDown: {
            type: 'integer',
            description: '跌停股票数量'
          },
          yesterdayLimitUpTodayFall: {
            type: 'integer',
            description: '昨日涨停今日跟跌股票数量'
          },
          distribution: {
            $ref: '#/components/schemas/RiseFallDistribution'
          },
          totalStocks: {
            type: 'integer',
            description: '总股票数量'
          },
          timestamp: {
            type: 'string',
            format: 'date-time',
            description: '数据时间戳'
          }
        }
      },

      // 涨跌停统计响应模型
      LimitUpDownResponse: {
        type: 'object',
        properties: {
          success: {
            type: 'boolean',
            example: true
          },
          message: {
            type: 'string',
            example: 'success'
          },
          data: {
            type: 'object',
            properties: {
              limitUp: {
                type: 'object',
                properties: {
                  count: {
                    type: 'integer',
                    example: 15,
                    description: '涨停股票数量'
                  },
                  stocks: {
                    type: 'array',
                    items: {
                      type: 'object',
                      properties: {
                        stockId: {
                          type: 'integer',
                          example: 1,
                          description: '股票ID'
                        },
                        stockCode: {
                          type: 'string',
                          example: '000001',
                          description: '股票代码'
                        },
                        stockName: {
                          type: 'string',
                          example: '平安银行',
                          description: '股票名称'
                        },
                        marketCode: {
                          type: 'string',
                          example: 'SZ',
                          description: '市场代码'
                        },
                        currentPrice: {
                          type: 'string',
                          example: '13.24',
                          description: '当前价格'
                        },
                        changePercent: {
                          type: 'string',
                          example: '10.00',
                          description: '涨跌幅%'
                        },
                        volume: {
                          type: 'string',
                          example: '1234567890',
                          description: '成交量'
                        },
                        turnover: {
                          type: 'string',
                          example: '163876543.21',
                          description: '成交额'
                        }
                      }
                    }
                  }
                }
              },
              limitDown: {
                type: 'object',
                properties: {
                  count: {
                    type: 'integer',
                    example: 8,
                    description: '跌停股票数量'
                  },
                  stocks: {
                    type: 'array',
                    items: {
                      type: 'object',
                      properties: {
                        stockId: {
                          type: 'integer',
                          description: '股票ID'
                        },
                        stockCode: {
                          type: 'string',
                          description: '股票代码'
                        },
                        stockName: {
                          type: 'string',
                          description: '股票名称'
                        },
                        marketCode: {
                          type: 'string',
                          description: '市场代码'
                        },
                        currentPrice: {
                          type: 'string',
                          description: '当前价格'
                        },
                        changePercent: {
                          type: 'string',
                          description: '涨跌幅%'
                        },
                        volume: {
                          type: 'string',
                          description: '成交量'
                        },
                        turnover: {
                          type: 'string',
                          description: '成交额'
                        }
                      }
                    }
                  }
                }
              },
              totalStocks: {
                type: 'integer',
                example: 4500,
                description: '总股票数量'
              },
              timestamp: {
                type: 'string',
                format: 'date-time',
                description: '数据时间戳'
              }
            }
          },
          timestamp: {
            type: 'string',
            format: 'date-time'
          }
        }
      },

      // 昨日涨停今日收益统计响应模型
      YesterdayLimitReturnResponse: {
        type: 'object',
        properties: {
          success: {
            type: 'boolean',
            example: true
          },
          message: {
            type: 'string',
            example: 'success'
          },
          data: {
            type: 'object',
            properties: {
              summary: {
                type: 'object',
                properties: {
                  totalYesterdayLimitUp: {
                    type: 'integer',
                    example: 45,
                    description: '昨日涨停股票总数'
                  },
                  todayRise: {
                    type: 'integer',
                    example: 12,
                    description: '今日上涨数量'
                  },
                  todayFall: {
                    type: 'integer',
                    example: 25,
                    description: '今日下跌数量'
                  },
                  todayFlat: {
                    type: 'integer',
                    example: 8,
                    description: '今日平盘数量'
                  },
                  riseRate: {
                    type: 'string',
                    example: '26.67%',
                    description: '上涨比例'
                  },
                  fallRate: {
                    type: 'string',
                    example: '55.56%',
                    description: '下跌比例'
                  }
                }
              },
              stocks: {
                type: 'array',
                items: {
                  type: 'object',
                  properties: {
                    stockId: {
                      type: 'integer',
                      example: 1,
                      description: '股票ID'
                    },
                    stockCode: {
                      type: 'string',
                      example: '000001',
                      description: '股票代码'
                    },
                    stockName: {
                      type: 'string',
                      example: '平安银行',
                      description: '股票名称'
                    },
                    marketCode: {
                      type: 'string',
                      example: 'SZ',
                      description: '市场代码'
                    },
                    yesterdayChangePercent: {
                      type: 'string',
                      example: '10.00',
                      description: '昨日涨幅%'
                    },
                    yesterdayClosePrice: {
                      type: 'string',
                      example: '12.04',
                      description: '昨日收盘价'
                    },
                    todayCurrentPrice: {
                      type: 'string',
                      example: '13.24',
                      description: '今日当前价'
                    },
                    todayChangePercent: {
                      type: 'string',
                      example: '9.97',
                      description: '今日涨跌幅%'
                    },
                    returnType: {
                      type: 'string',
                      enum: ['rise', 'fall', 'flat'],
                      example: 'rise',
                      description: '今日表现类型：rise-上涨，fall-下跌，flat-平盘'
                    }
                  }
                }
              },
              timestamp: {
                type: 'string',
                format: 'date-time',
                description: '数据时间戳'
              }
            }
          },
          timestamp: {
            type: 'string',
            format: 'date-time'
          }
        }
      },

      // ===== AI分析相关Schema定义 =====
      
      // 股票AI分析结果数据模型
      StockAIAnalysisData: {
        type: 'object',
        properties: {
          overallScore: {
            type: 'integer',
            minimum: 1,
            maximum: 10,
            example: 7,
            description: 'AI综合评分(1-10分)'
          },
          investmentAdvice: {
            type: 'string',
            example: '建议谨慎持有，短期内可能面临回调压力',
            description: '投资建议'
          },
          riskLevel: {
            type: 'string',
            example: '中等风险',
            description: '风险等级'
          },
          technicalAnalysis: {
            type: 'string',
            example: '技术指标显示股价处于超买状态，MACD出现顶背离信号',
            description: '技术面分析'
          },
          fundamentalAnalysis: {
            type: 'string',
            example: '公司基本面良好，市盈率合理，但行业增长放缓',
            description: '基本面分析'
          },
          marketSentiment: {
            type: 'string',
            example: '市场情绪偏向谨慎，资金流入有所减少',
            description: '市场情绪分析'
          },
          keyFactors: {
            type: 'array',
            items: {
              type: 'string'
            },
            example: ['行业政策利好', '业绩增长稳定', '技术指标超买'],
            description: '关键影响因素'
          },
          targetPriceRange: {
            type: 'string',
            example: '25.5-28.0元',
            description: '目标价位区间'
          },
          confidenceLevel: {
            type: 'integer',
            minimum: 1,
            maximum: 10,
            example: 8,
            description: '信心指数(1-10)'
          },
          analysisDate: {
            type: 'string',
            format: 'date-time',
            description: '分析生成时间'
          },
          stock: {
            type: 'object',
            properties: {
              id: {
                type: 'integer',
                description: '股票ID'
              },
              stockCode: {
                type: 'string',
                description: '股票代码'
              },
              stockName: {
                type: 'string',
                description: '股票名称'
              },
              marketCode: {
                type: 'string',
                description: '市场代码'
              }
            }
          }
        }
      },

      // 大分类AI分析结果数据模型
      CategoryAIAnalysisData: {
        type: 'object',
        properties: {
          marketScore: {
            type: 'integer',
            minimum: 1,
            maximum: 10,
            example: 6,
            description: '市场整体评分(1-10分)'
          },
          trendDirection: {
            type: 'string',
            example: '震荡偏强',
            description: '趋势方向'
          },
          investmentStrategy: {
            type: 'string',
            example: '建议采用均衡配置策略，关注优质成长股',
            description: '投资策略建议'
          },
          hotSectors: {
            type: 'array',
            items: {
              type: 'string'
            },
            example: ['新能源', '人工智能', '医疗健康'],
            description: '热点板块'
          },
          riskWarnings: {
            type: 'array',
            items: {
              type: 'string'
            },
            example: ['全球经济不确定性', '政策调整风险', '流动性收紧'],
            description: '风险提示'
          },
          opportunities: {
            type: 'array',
            items: {
              type: 'string'
            },
            example: ['政策红利释放', '技术突破带来新增长点', '估值修复机会'],
            description: '投资机会'
          },
          technicalIndicators: {
            type: 'string',
            example: '大盘指数处于箱体震荡，成交量温和放大',
            description: '技术指标分析'
          },
          capitalFlow: {
            type: 'string',
            example: '北向资金持续流入，机构资金偏谨慎',
            description: '资金流向分析'
          },
          analysisDate: {
            type: 'string',
            format: 'date-time',
            description: '分析生成时间'
          },
          category: {
            type: 'object',
            properties: {
              key: {
                type: 'string',
                example: 'a_stock',
                description: '大分类键值'
              },
              name: {
                type: 'string',
                example: 'A股市场',
                description: '大分类名称'
              },
              description: {
                type: 'string',
                example: '中国内地A股市场',
                description: '大分类描述'
              }
            }
          }
        }
      },

      // 股票AI分析响应模型
      StockAIAnalysisResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                $ref: '#/components/schemas/StockAIAnalysisData'
              }
            }
          }
        ]
      },

      // 大分类AI分析响应模型
      CategoryAIAnalysisResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                $ref: '#/components/schemas/CategoryAIAnalysisData'
              }
            }
          }
        ]
      },

      // 批量AI分析结果模型
      BatchAIAnalysisResult: {
        type: 'object',
        properties: {
          success: {
            type: 'integer',
            example: 8,
            description: '成功分析的数量'
          },
          failed: {
            type: 'integer',
            example: 2,
            description: '分析失败的数量'
          },
          total: {
            type: 'integer',
            example: 10,
            description: '总数量'
          },
          failedItems: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                id: {
                  oneOf: [
                    { type: 'integer' },
                    { type: 'string' }
                  ],
                  description: '失败项目的ID'
                },
                error: {
                  type: 'string',
                  description: '失败原因'
                }
              }
            },
            description: '失败项目详情'
          }
        }
      },

      // 批量AI分析响应模型
      BatchAIAnalysisResponse: {
        allOf: [
          { $ref: '#/components/schemas/ApiResponse' },
          {
            type: 'object',
            properties: {
              data: {
                $ref: '#/components/schemas/BatchAIAnalysisResult'
              }
            }
          }
        ]
      }
    }
  }
};

// Swagger选项
const swaggerOptions = {
  definition: swaggerDefinition,
  apis: [
    './routes/*.ts',
    './routes/*.js',
    './server.ts'
  ]
};

// 生成Swagger文档
export const swaggerSpec = swaggerJSDoc(swaggerOptions);